package dp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * 读取配置文件dp-config.properties
 * @author chen
 */
public class Config {
	//配置文件名,放在WEB-INF/classes/下
	private static final String CONFIG_FILENAME = "/dp-config.properties";
	//配置文件的最后读取使劲,超过3分钟才重读
	private static long lastReadTime = -1;
	//配置文件的最后修改时间 ，大于最后修改时间才重读
    private static long lastModifiedTime = -1;
    
    private static Map<String, String> config_map = new HashMap<String, String>();
    
    private static void reload(){
    	//tomcat6+2008读取不到文件
    	//URL url = Config.class.getResource(CONFIG_FILENAME);
    	//if(url==null){
    	//	throw new RuntimeException("没找到配置文件:"+CONFIG_FILENAME);
    	//}
    	//获得WEB-INF/classes的绝对路径，加载配置文件
//    	String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
//    	String path = "";
//    	try {
//			path = Config.class.getClassLoader().getResource("").toURI().getPath();
//		} catch (URISyntaxException e1) {
//			e1.printStackTrace();
//		}
//    	path += CONFIG_FILENAME;
    	//配置文件的最后修改时间
//    	File file = new File(path);
//    	if(!file.exists()){
//    		throw new RuntimeException("没找到配置文件:"+CONFIG_FILENAME+",路径="+path);
//    	}
    	
    	//间隔时间小于三分钟不重新加载
    	if(System.currentTimeMillis()-lastReadTime<180000){
    		return;
    	}
    	
    	synchronized(Config.class){
	    	File file = null;
	    	Resource cp = new ClassPathResource(CONFIG_FILENAME);
	    	if(!cp.exists()){
	    		throw new RuntimeException("没找到配置文件:"+CONFIG_FILENAME);
	    	}
	    	else{
	    		try {
					file = cp.getFile();
				} catch (IOException e) {
					e.printStackTrace();
					throw new RuntimeException("配置文件读取错误:"+CONFIG_FILENAME);
				}
	    	}
	    	
	    	long modifiedTime = file.lastModified();
	    	//发生过修改则重新加载配置文件
	    	if(modifiedTime>lastModifiedTime){
	    		InputStream in = null;
	            try {
	            	in = new FileInputStream(file);
	            	Properties p = new Properties();
	                p.load(in);
	                Enumeration<?> en = p.propertyNames();
	                while(en.hasMoreElements()){
	                	String key = (String)en.nextElement();
	                	config_map.put(key, p.getProperty(key).trim());
	                }
	                //记录最后修改时间
	                lastModifiedTime = modifiedTime;
	                //记录最后读取时间
	                lastReadTime = System.currentTimeMillis();
	            } catch (Exception e) {
	            	throw new RuntimeException("配置文件" + CONFIG_FILENAME + "加载错误:"+ e.getMessage());
	            } finally {
	            	//关闭inputstream
	            	if(in!=null){
	            		try{
	            			in.close();
	            		}catch(Exception e){}
	            	}
	            }
	    	}
    	}
    }

	public static String getConfigValue(String key){
		reload();
		return config_map.get(key);
	}
	
	public static Map<String, String> getConfigMap(){
		reload();
		return config_map;
	}
    
}
